package com.guet.news_main.controller;

import com.guet.common.utils.R;
import com.guet.news_main.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Classname ContentService
 * @Description done
 * @Date 2021/12/9 17:19
 * @Author csp
 */
@RestController
@RequestMapping("/news_main")
@CrossOrigin
public class ContentController {
    @Autowired
    private ContentService contentService;

    @GetMapping("/search/{keyword}/{pageIndex}/{pageSize}")
    public R parse(@PathVariable("keyword") String keyword,
                                           @PathVariable("pageIndex") Integer pageIndex,
                                           @PathVariable("pageSize") Integer pageSize) throws IOException {
        List<Map<String, Object>> search = contentService.search(keyword, pageIndex, pageSize);
        return R.ok().data("rows",search.subList((pageIndex-1)*pageSize,Math.min(search.size(),pageIndex*pageSize)))
                .data("total",search.size());
    }
}